import { createSlice } from '@reduxjs/toolkit'
const counterStore = createSlice({
  name: 'counter',
  //初始化状态
  initialState: {
    channelList: []
  },
  //修改状态的方法,同步方法，支持直接修改
  reducers: {
    setChannels: (state, action) => {
      state.channelList = action.payload
    }
  }
})
//解构出来actionCreater函数

const { setChannels } = counterStore.actions
const reducer = counterStore.reducer
const URL = 'http://geek.itheima.net/v1_0/channels'
const getList =()=> {
  return async (dispatch) => {
    const res = await fetch(URL)
    const jsonRes = await res.json()
    dispatch(setChannels(jsonRes.data.channels))
  }
}
export { getList }
//导出
export default reducer
